package com.stu.work.servlet;

import com.github.pagehelper.PageHelper;
import com.stu.work.mapper.IAdminMapper;
import com.stu.work.mapper.IBookMapper;
import com.stu.work.vo.AdminAndBookAndNoticeVo;
import com.stu.work.vo.BookAndAdminAndTypeVo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;

/**
 * ClassName: BookServlet
 * Package: com.stu.work.servlet
 * Description: @Version 1.0
 */
@WebServlet("/book")
public class BookServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter printWriter=response.getWriter();

        SqlSession session=null;
        String resource = "mybatis-config.xml";
        // 读取总配置文件
        InputStream inputStream = Resources.getResourceAsStream(resource);
        // 获取session工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 获取session
        session = sqlSessionFactory.openSession();

        Integer pageNum=1;
        Integer pageSize=3;

        PageHelper.startPage(pageNum,pageSize);
        List<BookAndAdminAndTypeVo> bookAndAdminAndType = session.getMapper(IBookMapper.class).getBookAndAdminAndType();
        pageNum++;
        for (BookAndAdminAndTypeVo eve:bookAndAdminAndType ) {
            printWriter.println(eve+"<br>");
        }
        PageHelper.startPage(pageNum,pageSize);
        bookAndAdminAndType = session.getMapper(IBookMapper.class).getBookAndAdminAndType();
        for (BookAndAdminAndTypeVo eve:bookAndAdminAndType ) {
            printWriter.println(eve+"<br>");
        }
        session.commit();
    }
}
